package com.neuedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neuedu.pojo.Result;
import com.neuedu.pojo.TAccount;
import com.neuedu.pojo.TAdministrator;
import com.neuedu.pojo.TElder;
import com.neuedu.pojo.dto.elder.AddElderDTO;
import com.neuedu.pojo.dto.elder.CheckinElderDTO;
import com.neuedu.service.ITAccountService;
import com.neuedu.service.ITElderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhangsan
 * @since 2024-06-16
 */
@RestController
@RequestMapping("/elder")
public class TElderController {
    @Autowired
    ITElderService elderService;
    @Autowired
    private ITAccountService itAccountService;

    @GetMapping("/page")
    public Result<List<TElder>> getPage(@RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                        // 需要条件查询的字段数据
                                        @RequestParam(name = "eldName", defaultValue = "", required = false) String eldName,
                                        @RequestParam(name = "eldHealth", defaultValue = "", required = false) Integer eldHealth,
                                        @RequestParam(name = "eldBed", defaultValue = "", required = false) Integer eldBed) {
        return elderService.getPage(currentPage, pageSize, eldName, eldHealth, eldBed);
    }

    @GetMapping("/query1/{eldId}") // 老人个人中心
    public Result findByElderId(@PathVariable("eldId") int eldId) {
        return elderService.findByElderId(eldId);
    }

    @GetMapping("/query2/{nurseId}") // 护理人员查所属老人
    public Result<List<TElder>> findByNurseId(@PathVariable("nurseId") Long nurseId, Long eldId, Boolean eldHealth) {
        return elderService.findByNurseId(nurseId, eldId, eldHealth);
    }

    @PostMapping("/add") // 注册时添加老人
    public Result addElderAcc(@RequestBody AddElderDTO addElderDTO) {
        TAccount tAccount = itAccountService.getBaseMapper().selectOne(new LambdaQueryWrapper<TAccount>()
                .eq(TAccount::getAccUsername, addElderDTO.getAccUsername()));
        if (tAccount != null) {
            return Result.fail("用户名已存在");
        }
        return elderService.addElderAcc(addElderDTO);
    }

    @PostMapping("/mod") // 入住登记
    public Result modElder(@RequestBody CheckinElderDTO dto) {
        return elderService.checkinElder(dto);
    }

    @GetMapping("/checkin") // 查询入住登记信息
    public Result checkinInfo(Long accId,String typeofrecord) {
        return elderService.getCheckinInfo(accId,typeofrecord);
    }

    /**
     * 获取老人状态
     */
    @GetMapping("/state")
    public Result getSateByUserName(String userName){
        TAccount tAccount = itAccountService.getBaseMapper().selectOne(new LambdaQueryWrapper<TAccount>()
                .eq(TAccount::getAccUsername, userName));
        TElder tElder = elderService.getBaseMapper().selectOne(new LambdaQueryWrapper<TElder>()
                .eq(TElder::getAccId, tAccount.getAccId()));
        return Result.success(tElder,"获取状态成功");
    }

    @GetMapping("/querybybed")// 查床位号为空的老人
    public Result findByBed(){
        return elderService.findByBed();
    }

    @PostMapping("/modbed")// 分配老人床号
    public Result modBedByEldId(@RequestParam(name = "eldId")Integer eldId,
                                @RequestParam(name = "bedNumber")String bedNumber){
        return elderService.modBedByEldId(eldId,bedNumber);
    }
    @GetMapping("/querybybednumber")// 根据床号查询老人
    public Result findElderByBedNumber(@RequestParam(name = "bedNumber")String bedNumber){
        return elderService.findElderByBedNumber(bedNumber);
    }
    @GetMapping("/queryall")
    public Result findAllWithNurse(){
        return elderService.findAllWithNurse();
    }
    @PostMapping("/modnurseid") //绑定护理人员id
    public Result modNurseIdById(@RequestParam(name = "nurseId")Integer nurseId,
                                  @RequestParam(name = "elderId")Integer eldId){
        return elderService.modNurseIdById(nurseId,eldId);
    }



    @PostMapping("/edit")
    public Result modElderById(@RequestBody TElder elder) {
        // 调用服务层方法进行更新
        return elderService.modElderById(elder);
    }

    /**
     * 依照账户ID获取老人信息
     */
    @GetMapping("/querybyaccid/{accId}")
    public Result findByAccId(@PathVariable("accId") Long accId) {
        return elderService.findByAccId(accId);
    }
    /**
     * 查找所有老人信息
     */
    @GetMapping("/querybyaccid/all")
    public Result<List<TElder>> findAll() {
        return elderService.findAll();
    }

}
